import java.util.*;


public class UserManager{

	private User user;
	private List<User> users;

	
	public UserManager()
	{
		user=null;
		users=SQLConnector.getUsers();

	}
	public boolean logIn(String username, String password)
	{
		user=new User(null,null,username,password);
		user=users.get(search(username));

		return true;
	}
	public int search(String username)
	{
		Comparator<? super User> c = new UserComparator();
		Collections.sort(users, c);
		return Collections.binarySearch(users, user, c);
	}
	public User makeUser(String name, String email, String username, String password)
	{
		user=new User(name, email, username, password);
		if (users.contains(user));
			SQLConnector.storeUser(user);
		return user;
	}
	public void deleteUser()
	{
		/* Deleting a user must be followed by the deletion of the users' tasks and categories ordered by UI class. */
		SQLConnector.deleteUser(user);
		users.remove(user);
		user=null;
	}
	/* If possible to edit SQLite's XML values, then deleteUser and makeUser rendered obselete */
	public User updateUser(String name, String email, String username, String password)
	{
		deleteUser();
		return makeUser(name, email, username, password);
	}

	/* Private inner class for comparing two Users when searching for one */
	private class UserComparator implements Comparator<User>{
		@Override
		public int compare(User user1, User user2) {
	        return (user1.compareTo(user2)<0 ? -1 : (user1.compareTo(user2)==0 ? 0 : 1));
		}
	}


	}
